<?php
/**
 * This is media/admin_index template file. 
 *
 * @copyright     Copyright 2008-2010, LilCake.net
 * @link          http://lilcake.net LilCake.net
 * @package       lil_media
 * @subpackage    lil_media.views.areas
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
	echo $this->Html->css('/lil_media/css/lil_media');
	
	$paths = Set::filter(explode(DS, $base_path)); $path = '';
	
	foreach ($paths as $i => $part) {
		$path .= DS . $part;
		$paths[$i] = $this->Html->link($part, array('path' => base64_encode($path)));
	}
	

	$media_index = array(
		'title_for_layout' => __d('lil_media', 'Media Index'),
		'menu' => array(
			'add' => array(
				'title' => __d('lil_media', 'Upload', true),
				'visible' => true,
				'url'   => array(
					'admin'      => true,
					'plugin'     => 'lil_media',
					'controller' => 'media',
					'action'     => 'add',
					'path' => empty($base_path) ? null : base64_encode($base_path)
				)
			)
		),
		'actions' => array(
			'pre' => '<div>',
			'post' => '</div><br />',
			'lines' => array(
				$this->Html->link('root', array('path' => null)) . DS . implode(' ' . DS . ' ', $paths)
			)
		),
		'panels' => array(
			'index' => array(
				'id' => 'MediaIndex',
				'lines' => array(
				)
			)
		)
	);
	foreach ($folders as $folder) {
		$media_index['panels']['index']['lines'][] = sprintf('<div class="media_item">%1$s<div class="media_name">%2$s</div></div>',
			$this->Html->link(
				$this->Html->image('/lil_media/img/folder.gif'),
				array(
					'action' => 'index',
					'path'   => base64_encode($base_path . $folder)
				),
				array(
					'escape' => false,
					'class'  => 'lil-media-item lil-media-folder',
					'title'  => $folder
				)
			),
			$this->Html->link(
				$folder,
				array(
					'action' => 'index',
					'path'   => base64_encode($base_path . $folder)
				),
				array(
					'title' => $folder
				)
			)
		);
	}
	
	foreach ($data as $item) {
		$path_parts = pathinfo($base_path . DS . $item['filename']);
		
		$icon = ''; $item_url = ''; $item_class = '';
		$item_title = $www_dir . $path_parts['basename'];
		
		if (in_array(strtolower($path_parts['extension']), array('gif', 'png', 'jpg'))) {
			$icon = $this->Html->image($www_dir . $path_parts['basename']);
			$item_class = ' lil-media-image';
		} else if (in_array(strtolower($path_parts['extension']), array('doc', 'docx'))) {
			$icon = $this->Html->image('/lil_media/img/doc.gif');
		} else if (in_array(strtolower($path_parts['extension']), array('xls', 'xlsx'))) {
			$icon = $this->Html->image('/lil_media/img/xls.gif');
		} else if (strtolower($path_parts['extension']) == 'pdf') {
			$icon = $this->Html->image('/lil_media/img/pdf.gif');
		} else {
			$icon = $this->Html->image('/lil_media/img/x.gif');
		}
		
		
		if (empty($item_url)) {
			$item_url = array(
				'action'   => 'view',
				'path'     => (!empty($this->params['named']['path'])) ?
					$this->params['named']['path'] : null,
				'filename' => base64_encode($path_parts['basename'])
			);
		}
		
		$media_index['panels']['index']['lines'][] = sprintf('<div class="media_item"><div class="media_size">%1$s</div>%2$s<div class="media_name">%3$s</div></div>',
			$this->Number->toReadableSize($item['size']),
			$this->Html->link(
				$icon,
				$item_url,
				array(
					'escape' => false,
					'class'  => 'lil-media-item' . $item_class,
					'title'  => $item_title
				)
			),
			$this->Html->link(
				$path_parts['basename'],
				array(
					'action' => 'view',
					'path'   => (!empty($this->request->params['named']['path'])) ?
						$this->request->params['named']['path'] : null,
					'filename'   => base64_encode($path_parts['basename'])
				),
				array(
					'title' => $path_parts['basename']
				)
			)
		);
		
	}
	
	$media_index = $this->callPluginHandlers('lil_media_media_admin_index', array('data' => $data, 'contents' => $media_index));
	$this->Lil->panels($media_index['contents']);